package br;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class trasam
 */
@WebServlet("/trasam")
public class trasam extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public trasam() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 HttpSession sesja = request.getSession(true); 
		PrintWriter out = response.getWriter();
		System.out.println(sesja.getAttribute("log")+" "+sesja.getAttribute("nick"));
		if(sesja.getAttribute("log").equals("kurier")){ 
		 String nick = (String)sesja.getAttribute("nick");
		 System.out.println(nick);
		 String odp="";
			try {
			  Connection con =mb.commons.DbConnection.getConnection();
			  Statement stmt = con.createStatement();
			  ResultSet rs = stmt.executeQuery("SELECT * FROM zamowienia, kurierzy,uzytkownicy,oddzialy,paczki WHERE uzytkownicy.login='"+nick+"' AND kurierzy.id = uzytkownicy.id AND kurierzy.id_kuriera = zamowienia.id_kuriera AND kurierzy.id_oddzialu=oddzialy.id AND paczki.id_paczki=zamowienia.id_paczki AND zamowienie.status='w trakcie dostarczania'");
			  
			  while(rs.next()){
				  if(rs.isFirst()){
				  odp+=rs.getString("oddzialy.adres")+"&";
				  }
				 		odp+=rs.getString("paczki.adres2")+" , "+rs.getString("paczki.miejscowosc2")+"&"; 
			  }	  
			  out.println(odp);
			  
			} catch (Exception e) {
			}
	}
	}

}
